import { message } from 'antd';
import fetch from '../utils/request';

const queryFansManageList = ({ payload }) => fetch.post('/haierp1/wx/queryFans', { data: payload }).catch(e => e);

export default {
  namespace: 'fansManage',
  state: {
    fansList: [],
    fansPage: 1,
    fansPageSize: 20,
    fansTotal: 1,
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/fansManage/fansManageList' && !window.existCacheState('/fansManage/fansManageList')) {
          setTimeout(() => {
            dispatch({ type: 'queryFansManageList', payload: {} });
          }, 0);
        }
      });
    },
  },

  effects: {
    * queryFansManageList({ payload }, { call, put, select }) { // SKU管理列表
      let pageIndex = yield select(({ fansManage }) => fansManage.fansPage);
      let pageSize = yield select(({ fansManage }) => fansManage.fansPageSize);
      if (payload.pageIndex) {
      	pageIndex = payload.pageIndex;
        yield put({ type: 'saveFansPage', payload });
      }
      if (payload.pageSize) {
        pageSize = payload.pageSize;
        yield put({ type: 'saveFansPageSize', payload });
      }
      const data = yield call(queryFansManageList, { payload: { ...payload, pageIndex, pageSize } });
      if (data.success) {
        yield put({ type: 'updateFansList', payload: data, });
      }
    },
  },

  reducers: {
    saveFansPage(state, { payload }) {
      return { ...state, fansPage: payload.pageIndex };
    },
    saveFansPageSize(state, { payload }) {
      return { ...state, fansPageSize: payload.pageSize };
    },
    updateFansList(state, { payload }) {
      return { ...state, fansList: payload.data, fansTotal: payload.totalCount };
    },
  },
};
